//
//  KINGUIImage+Extension.swift
//  照片选择
//
//  Created by king on 15/8/7.
//  Copyright © 2015年 king. All rights reserved.
//

import UIKit

extension UIImage {
    func scaleImage(width:CGFloat) -> UIImage {
        if size.width < width {
            return self
        }
        //根据传递进来的width计算出高度
        let height = size.height * width/size.width
        let s = CGSizeMake(width, height)
        //利用图片上下文进行
        UIGraphicsBeginImageContext(s)
        //将图片会知道上下文中
        drawInRect(CGRect(origin: CGPointZero, size: s))
        //获取到上下文中的图片
        let result = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        //返回结果
        return result
    }
}
